05 - Java Inheritance, Abstract Сlasses
05 - Java Inheritance, Abstract Сlasses
Abstract classes
IT Academy
Agenda
• Inheritance • Keyword final
• Access Modifiers • Abstract class
• Methods Overriding • Composition
• Keyword super • Reference Types
Inheritance
• Inheritance in Java is form of software reusability:
• new classes created from existing ones;
• absorb attributes and behaviors, and add in their own.
• Subclass inherits from superclass:
• direct superclass – subclass explicitly inherits;
• indirect superclass – subclass inherits from two or more levels up the class
hierarchy.
Inheritance
• Example
public class Rectangle {
public int width;
public int height;
• To inherit the properties and methods of a class you use the extends
keyword.
privateint
public intgetArea()
getArea(){{
return (int) (width * height * Math.sin(angle * Math.PI / 180));
}
}
Keyword super
• A constructor can call another constructor in its superclass using the keyword
super and the parameters list.
public Rectangle(int w, int h) {
width = w;
height = h; public Parallelogram(int w, int h, int a) {
} super(w, h);
angle = a;
}
• The keyword super also used for access original superclass method.
public int getArea() {
if (angle == 90) {
return super.getArea();
}
return (int) (width * height * Math.sin(angle * Math.PI / 180));
}
Keyword final
• Declaring variables final:
• can only be initialized once;
swap
w 10
h 25
temp 25
main
width 25
height 10
swap
temp 25 Rectangle
Date
rect ref width 10 Wed Dec 15
height 25 11:26:43
main crtDate ref
rectangle ref
Stack Memory
Heap Memory
Passing Arguments to Method
• All object references in Java are passed by value.
• This means that a copy of the value will be passed to a method.