0% found this document useful (0 votes)
26 views

05 - Java Inheritance, Abstract Сlasses

The document discusses inheritance, abstract classes, and composition in object-oriented programming. It defines inheritance as a way to create new classes from existing ones by inheriting attributes and behaviors. Abstract classes are introduced as classes that cannot be instantiated and must be subclassed. Composition is defined as an alternative to inheritance for implementing relationships between objects using instance variables.

Uploaded by

Mykola Demchyna
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

05 - Java Inheritance, Abstract Сlasses

The document discusses inheritance, abstract classes, and composition in object-oriented programming. It defines inheritance as a way to create new classes from existing ones by inheriting attributes and behaviors. Abstract classes are introduced as classes that cannot be instantiated and must be subclassed. Composition is defined as an alternative to inheritance for implementing relationships between objects using instance variables.

Uploaded by

Mykola Demchyna
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Inheritance,

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;

public int getPerimeter() {


return 2 * (width + height);
}
}

• To inherit the properties and methods of a class you use the extends
keyword.

public class Parallelogram extends Rectangle {


public int angle;
}
Inheritance
• Example

public class Main {


public static void main(String[] args) {

Rectangle rectangle = new Rectangle();


rectangle.width = 42;
rectangle.height = 74; Perimeter of parallelogram equals 232.0

Parallelogram parallelogram = new Parallelogram();


parallelogram.width = 42; // inherit from Rectangle
parallelogram.height = 74; // inherit from Rectangle
parallelogram.angle = 35;

double p = parallelogram.getPerimeter(); // inherit from Rectangle


System.out.println("Perimeter of parallelogram equals " + p);
}
}
Inheritance in real world
• Example
Access Modifiers
• Java provides a number of Access Modifiers to set access levels for classes,
variables, methods, and constructors.

• There are 4 types of access levels:


• public – visible to the everywhere
• private – visible only in the same class
• default (package-private) – visible within the
package level
• protected – within package and outside the
package but need to use inheritance then only.
The protected Access Modifier
• Variables, methods, and constructors, which are declared protected in a
superclass can be accessed only by the subclasses in other package or any
class within the package.
• The protected access modifier cannot be applied to class and interfaces.
• Example: class Rectangle {
protected int width;
protected int height;
// getters and setters
}

class Parallelogram extends Rectangle {


private int angle;
// getters and setters
public int getArea() {
return (int) (width * height * Math.sin(angle * Math.PI / 180));
}
}
Access to Class Members
Inheritance and Methods Overriding
• A subclass can modify behavior inherited from a parent class.
• A subclass can create a method with different functionality than the parent‘s
method but with the same signature.
class Rectangle {
protected int width;
protected int height;

public int getPerimeter() { return 2 * (width + height); }

public int getArea() { return width * height; }


} The access modifier of an overriding or hiding
method must provide at least as much access
class Parallelogram extends Rectangle { as the overridden or hidden method.
private int angle;

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;

final Rectangle rectangle = new Rectangle(21, 47);


rectangle.setWidth(36);
rectangle = new Rectangle(18, 53);

• Declaring methods final:


• cannot be overridden in a subclass (static and private methods are implicitly
final);
• program can inline final methods (early binding ).
• Declaring classes final:
• cannot be a superclass (all methods in class are implicitly final).
Abstract Classes
• A class can be declared as Abstract when we need to forbid creating instances
of this class.
• Abstract class may has one or more abstract methods.
• A method is declared abstract when it has a method heading, but no body –
which means that an abstract method has no implementation code inside
curly braces like normal methods do.
• The derived class must provide a definition method or must be declared
abstract itself.
• A non abstract class is called a concrete class.
Abstract Classes
abstract public class Shape {
private String name;

public Shape(String name) { this.name = name; }

public abstract double getArea();

protected void shapeInfo() {


System.out.println("This is " + name + ":");
}
} public class Square extends Shape {
private double side;

public Square(double side, String name) {


super(name);
this.side = side;
}
@Override
public double getArea() { return side * side; }
}
Composition
• Composition is the design technique to implement has-a relationship in
classes.
• Composition is achieved by using instance variables that refers to other
objects.

class Point { class Circle {


private int x, y; private Point point;
public Point(int x, int y) { private int radius;
this.x = x; public Circle(Point point, int radius) {
this.y = y; this.point = point;
} this.radius = radius;
} }
}

Circle circle = new Circle(new Point(74, 38), 26);


Inheritance vs. Composition
class Point {
protected int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
class Circle extends Point {
}
private int radius;
public Circle(int x, int y, int radius) {
super(x, y);
this.radius = radius;
}
}

Circle circle = new Circle(74, 38, 26);


Inheritance vs. Composition
• Kinds of Relationships between objects:
• "is a" - object of subclass "is a" object of the superclass (inheritance).
• "has a" – object "has a" object of another class as a member (composition);
Reference Types

• A Reference is a data element that holds the address of a memory location.


• A Reference Variable contains a “pointer” to an object.
Reference Types

• This statement has three parts:


• Declaring a Variable to refer to an Object;
• Instantiating a Class using new operator;
• Initializing an Object using the Constructor.
Objects and Assignment Operator
public class Main {
public static void main(String[] args) {
Rectangle rect1 = new Rectangle(30, 10);
Rectangle rect2 = new Rectangle(25, 15);

double result = rect1.getPerimeter();


boolean var = true;
}
}
Objects and Assignment Operator
public class Main {
public static void main(String[] args) {
Rectangle rect1 = new Rectangle(30, 10);
Rectangle rect2 = rect1;

double result = rect1.getPerimeter();


boolean var = true;
}
}
Passing Arguments to Method
public class Main {
public static void main(String[] args) {
int width = 25, height = 10;

System.out.println("width = " + width + "\theight = " + height);


swap(width, height);
System.out.println("width = " + width + "\theight = " + height);
}

public static void swap(int w, int h) {


int temp = w;
w = h;
h = temp;
System.out.println("width = " + w + "\theight = " + h);
}
}

Before calling method 'swap': width = 25, height = 10


Inside the method 'swap': width = 10, height = 25
After calling method 'swap': width = 25, height = 10
Passing Arguments to Method

swap
w 10
h 25
temp 25

main
width 25
height 10

Stack Memory Heap Memory


Passing Arguments to Method
public class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(25, 10);

System.out.println("Before calling method 'swap': width = " +


rectangle.width + ", height = " + rectangle.height);
swap(rectangle);
System.out.println("After calling method 'swap': width = " +
rectangle.width + ", height = " + rectangle.height);
}

public static void swap(Rectangle rect) {


int temp = rect.width;
rect.width = rect.height;
rect.height = temp;
System.out.println("Inside the method 'swap': width = " +
rect.width + ", height = " + rect.height);
}
}

Before calling method 'swap': width = 25, height = 10


Inside the method 'swap': width = 10, height = 25
After calling method 'swap': width = 10, height = 25
Passing Arguments to Method

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.

• Unfortunately, when we pass the object, we are passing the


copy of reference to it.
Thanks for
attention!
IT Academy

You might also like