160 likes | 290 Vues
This lesson on Java programming delves into inheritance, one of the core concepts of object-oriented programming. It reviews key topics such as the superclass and subclass relationship, method overriding, and the use of the `super` keyword. It also discusses abstract classes, their purpose, and how they differ from concrete classes. Through clear examples like BankAccount and CheckingAccount, learners will see how these concepts are implemented in Java, reinforcing the understanding of inheritance and its benefits in code reusability and organization.
E N D
Inheritance Lesson - 5 CSM-Java Programming-I Spring,2005
Objectives • Review of last class • Inheritance • super • Method Overriding • Abstract Classes CSM-Java Programming-I Lesson-1
Inheritance • When a class B acquires the properties of another class A, then class B is said to have inherited class A. • Here, class A is a superclass and class B is a subclass. • A subclass inherits all the instance variables and methods defined by the superclass and adds its own, unique elsements. • The keyword extends is used to inherit a class. Eg: class subclass extends superclass { CSM-Java Programming-I Lesson-1
Inheritance • Every class in Java is an extended class, whether it is declared with an extends keyword or not. • If a class does not explicitly extend from another class, it implicitly extends the Object class. CSM-Java Programming-I Lesson-1
Example: Inheritance public class BankAccount { int acctNum; String name; public BankAccount(int bAcctNum, String name) { acctNum = bAcctNum; ……. } public double getBalance () { …… } } CSM-Java Programming-I Lesson-1
Example: Inheritance public class CheckingAccount extends BankAccount { double interest; public CheckingAccount(int aNum, String aName, double aInt) { super (aNum, aName); interest = aInt; } public double calculateInterest() { ……. }} CSM-Java Programming-I Lesson-1
super • Whenever a subclass class super(), it is calling the constructor of its immediate superclass. • super() must be the first statement executed inside a subclass constructor. • super.member always uses the superclass’s member (member could be a method or an instance variable) CSM-Java Programming-I Lesson-1
Example: super public class BankAccount { protected double getBalance() { return 1000.00; } } public class SavingsAccount extends BankAccount{ protected double getBalance() { return 1010.00; } CSM-Java Programming-I Lesson-1
super protected void printBalance() { BankAccount ba = this; //superclass variable //can reference //subclass object. System.out.println(super.getBalance()); System.out.println(this.getBalance()); System.out.println(ba.getBalance()); } } Output: 1000.00 1010.00 1010.00 CSM-Java Programming-I Lesson-1
Method Overriding • When a method in a subclass has the same name and type signature and a method in its superclass, then the method in the subclass is said to override the method in the subclass. • Only non-static methods can be overridden. • Both signature and return type must be the same as the superclass. • The throws clause of an overriding method can have fewer types listed than the method in the superclass, or more specific types or both. CSM-Java Programming-I Lesson-1
Example: Method Overriding public class Parent { public void hello() { System.out.println(“Hello from parent”); } } public class Child extends Parent { public void hello() { System.out.println(“Hello from Child”); } } CSM-Java Programming-I Lesson-1
Method Overriding • An extended class can change the access of a superclass’s methods, but only if it provides more access. • A method declared protected in the super class can be redeclared protected or public, but not private. • Fields cannot be overridden; they can only be hidden. • To access the hidden fields use the super keyword. CSM-Java Programming-I Lesson-1
final and Inheritance • A method declared final cannot be overridden • A class can also be declared final. Such a class cannot be extended. final class Security { // } • A final class’s methods are implicitly final. • static and private methods cannot be overridden. CSM-Java Programming-I Lesson-1
Abstract Classes • A superclass that only defines a generalized form that will be shared by all its subclasses, leaving the implementation details to its subclasses is said to an abstract class. • A concrete class has concrete methods, including implementations of any abstract methods inherited from its superclasses. • Any class with abstract methods should be declared abstract. CSM-Java Programming-I Lesson-1
Example: Abstract Class abstract class Shape { abstract double area(); public void display () { // concrete method // Do something } } class Square extends Shape { double area() { // Do something } CSM-Java Programming-I Lesson-1
Abstract Classes • An abstract class cannot have objects because it is not complete, but it can have references. • Static methods and constructors cannot be declared abstract. • Any subclass of an abstract class must either implement all the abstract methods in the superclass or be itself declared abstract. • A concrete method can be overriden to become abstract. • It is illegal to declare a class both final and abstract. CSM-Java Programming-I Lesson-1