0% found this document useful (0 votes)
4 views31 pages

Oops Java

The document provides a comprehensive overview of Object-Oriented Programming (OOP) principles, including encapsulation, inheritance, polymorphism, and abstraction, specifically in the context of Java. It details the definitions of classes and objects, constructors, and provides examples of how to create and use them in Java code. Additionally, it highlights the advantages of OOP, such as modularity, reusability, scalability, data security, and flexibility.

Uploaded by

techgamil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views31 pages

Oops Java

The document provides a comprehensive overview of Object-Oriented Programming (OOP) principles, including encapsulation, inheritance, polymorphism, and abstraction, specifically in the context of Java. It details the definitions of classes and objects, constructors, and provides examples of how to create and use them in Java code. Additionally, it highlights the advantages of OOP, such as modularity, reusability, scalability, data security, and flexibility.

Uploaded by

techgamil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Contents

Denion o OOP (Objec-Oriened Programming)................................................................................ 1


Class and Objecs in Java......................................................................................................................... 2
Consrucors in Java ................................................................................................................................ 5
Objec Class in Java ...............................................................................................................................12
Absracon in Java ................................................................................................................................14
Encapsulaon in Java ............................................................................................................................17
Inheriance in Java ................................................................................................................................20
Access Speciers in Java........................................................................................................................25
Polymorphism in Java............................................................................................................................28

Definiton of OOP (Objec-Oriened Programming)


Objec-Oriened Programming (OOP) is a programming paradigm based on he concep o "objecs,"
which can conain daa (in he orm o elds or atribues) and code (in he orm o mehods or
uncons). OOP emphasizes he principles o encapsulaon, inheriance, absracon, and
polymorphism o design and build modular, reusable, and mainainable sofware sysems.

Key Principles of OOP

1. Encapsulaon:
Bundling daa (atribues) and mehods (uncons) ha operae on he daa ino a single uni
(class). I also involves resricng direc access o some componens using access modiers
(privae, proeced, public).

2. Inheriance:
A mechanism where one class (child) can acquire properes and behaviors o anoher class
(paren). This promoes code reuse and esablishes a hierarchical relaonship beween
classes.

3. Polymorphism:
The abiliy o a single uncon or objec o ake on mulple orms. This is ypically achieved
hrough mehod overloading (compile-me polymorphism) and mehod overriding (runme
polymorphism).

4. Absracon:
The process o hiding implemenaon deails and exposing only he necessary unconaliy.
Absrac classes and ineraces are ools used o achieve absracon.

Advanages of OOP
1. Modulariy: Code is organized ino classes, making i easy o undersand and manage.

2. Reusabiliy: Exisng classes can be reused and exended via inheriance.

3. Scalabiliy: Easier o scale and mainain applicaons as complexiy grows.

4. Daa Securiy: Encapsulaon helps proec sensive daa by resricng access.

5. Flexibiliy: Polymorphism allows mehods o handle dieren ypes o daa or behavior.

Real-World Example

A "Car" class in OOP could include:

• Atribues: brand, color, speed.

• Mehods: sarEngine(), accelerae(), brake().

Class and Objecs in Java


Denion:

• Class: A class is a blueprin or prooype rom which objecs are creaed. I denes properes
(variables) and behaviors (mehods) ha he objecs creaed rom he class will have.

• Objec: An objec is an insance o a class. When a class is dened, no memory is allocaed


unl an objec o ha class is creaed.

Class Synax:

class ClassName {

// Fields (Properes)

in eld1;

Sring eld2;

// Consrucor

public ClassName(in eld1, Sring eld2) {

his.eld1 = eld1;

his.eld2 = eld2;

// Mehod (Behavior)

public void display() {


Sys[Link].prinln("Field1: " + eld1 + ", Field2: " + eld2);

Creang Objecs:

To creae an objec o a class, you use he new keyword ollowed by he consrucor o he class.

ClassName objecName = new ClassName(argumens);

Example Code: Class and Objecs

// Class denion

class Car {

// Properes (Fields)

Sring model;

in year;

Sring color;

// Consrucor o inialize he properes

public Car(Sring model, in year, Sring color) {

[Link] = model;

[Link] = year;

[Link] = color;

// Mehod o display he deails o he car

public void displayDeails() {

Sys[Link].prinln("Car Model: " + model);

Sys[Link].prinln("Car Year: " + year);

Sys[Link].prinln("Car Color: " + color);

// Mehod o sar he car


public void sar() {

Sys[Link].prinln(model + " is sarng...");

public class Main {

public sac void main(Sring[] args) {

// Creang objecs (insances) o he Car class

Car car1 = new Car("Toyoa Corolla", 2020, "Red");

Car car2 = new Car("Honda Civic", 2021, "Blue");

// Calling mehods on car1 and car2 objecs

[Link]ails(); // Displaying deails o car1

car1.sar(); // Sarng car1

Sys[Link].prinln(); // Prin a blank line or separaon

[Link]ails(); // Displaying deails o car2

car2.sar(); // Sarng car2

Oupu:

yaml

Car Model: Toyoa Corolla

Car Year: 2020

Car Color: Red

Toyoa Corolla is sarng...

Car Model: Honda Civic

Car Year: 2021


Car Color: Blue

Honda Civic is sarng...

Consrucors in Java
Denion:

A consrucor in Java is a special mehod used o inialize objecs. I is called auomacally when an
objec o a class is creaed. The consrucor's primary purpose is o assign inial values o he elds
o a class.

Characeriscs of Consrucors:

1. Name: A consrucor has he same name as he class.

2. No Reurn Type: I does no have a reurn ype (no even void).

3. Auomac Invocaon: Called auomacally when an objec is creaed using he new
keyword.

4. Overloading: Mulple consrucors can be dened in a class (consrucor overloading) by


varying he number or ypes o parameers.

Types of Consrucors in Java:

1. Defaul Consrucor:

o A consrucor wihou any parameers.

o Auomacally provided by Java i no oher consrucor is dened.

o Inializes elds o heir deaul values (e.g., 0 or numbers, null or objecs, ec.).

Example:

class Suden {

Sring name;

in age;

// Deaul Consrucor

Suden() {

Sys[Link].prinln("Deaul consrucor called");

name = "Unknown";
age = 0;

void displayIno() {

Sys[Link].prinln("Name: " + name);

Sys[Link].prinln("Age: " + age);

public class Main {

public sac void main(Sring[] args) {

Suden suden = new Suden(); // Deaul consrucor called

suden.displayIno();

Oupu:

Deaul consrucor called

Name: Unknown

Age: 0

2. Parameerized Consrucor:

o A consrucor ha acceps parameers o inialize elds wih specic values.

o Useul or creang objecs wih dieren inial saes.

Example:

class Suden {

Sring name;

in age;

// Parameerized Consrucor

Suden(Sring name, in age) {

[Link] = name;
[Link] = age;

void displayIno() {

Sys[Link].prinln("Name: " + name);

Sys[Link].prinln("Age: " + age);

public class Main {

public sac void main(Sring[] args) {

Suden suden = new Suden("Anil", 20); // Parameerized consrucor called

suden.displayIno();

Oupu:

Name: Anil

Age: 20

3. Copy Consrucor:

o A consrucor ha creaes a new objec as a copy o an exisng objec.

o Java does no provide a deaul copy consrucor; you mus implemen i explicily.

Example:

class Suden {

Sring name;

in age;

// Parameerized Consrucor

Suden(Sring name, in age) {

[Link] = name;

[Link] = age;
}

// Copy Consrucor

Suden(Suden oher) {

[Link] = o[Link];

[Link] = o[Link];

void displayIno() {

Sys[Link].prinln("Name: " + name);

Sys[Link].prinln("Age: " + age);

public class Main {

public sac void main(Sring[] args) {

Suden suden1 = new Suden("Anil", 20);

Suden suden2 = new Suden(suden1); // Copy consrucor called

suden[Link]o();

Oupu:

Name: Anil

Age: 20

Key Poins o Remember:

1. his():
Used o call one consrucor rom anoher wihin he same class.
Example:

class Suden {

Sring name;

in age;
Suden() {

his("Deaul Name", 0); // Calls parameerized consrucor

Suden(Sring name, in age) {

[Link] = name;

[Link] = age;

void displayIno() {

Sys[Link].prinln("Name: " + name);

Sys[Link].prinln("Age: " + age);

public class Main {

public sac void main(Sring[] args) {

Suden suden = new Suden(); // Calls deaul consrucor

suden.displayIno();

2. super():
Used o call he paren class consrucor.
Example:

class Paren {

Paren() {

Sys[Link].prinln("Paren class consrucor called");

class Child exends Paren {


Child() {

super(); // Calls he Paren class consrucor

Sys[Link].prinln("Child class consrucor called");

public class Main {

public sac void main(Sring[] args) {

Child child = new Child();

Oupu:

kolin

Copy code

Paren class consrucor called

Child class consrucor called

Consrucor Overloading:

Java suppors dening mulple consrucors wih dieren parameer liss.

Example:

class Recangle {

in lengh, breadh;

// Consrucor wih no parameers

Recangle() {

[Link]h = 0;

[Link]h = 0;

// Consrucor wih one parameer

Recangle(in side) {
[Link]h = side;

[Link]h = side;

// Consrucor wih wo parameers

Recangle(in lengh, in breadh) {

[Link]h = lengh;

[Link]h = breadh;

in area() {

reurn lengh * breadh;

public class Main {

public sac void main(Sring[] args) {

Recangle r1 = new Recangle();

Recangle r2 = new Recangle(5);

Recangle r3 = new Recangle(4, 6);

Sys[Link].prinln("Area o r1: " + [Link]());

Sys[Link].prinln("Area o r2: " + [Link]());

Sys[Link].prinln("Area o r3: " + [Link]());

Oupu:

mahemaca

Copy code

Area o r1: 0

Area o r2: 25
Area o r3: 24

Objec Class in Java


Denion:

The Objec class in Java is he roo class o he Java class hierarchy. Every class in Java, eiher direcly
or indirecly, inheris rom he Objec class. This means ha all Java classes auomacally inheri he
mehods o he Objec class unless explicily overridden.

Key Characeriscs:

1. Roo Class: All classes in Java exend Objec implicily.

2. Common Mehods: Provides mehods ha are available o all Java objecs, such as
oSring(), equals(), hashCode(), ec.

3. Universal Paren: Even user-dened classes are derived rom he Objec class.

Mehods of he Objec Class

Mehod Descripon

oSring() Reurns a sring represenaon o he objec.

equals(Objec
Compares wo objecs or equaliy.
obj)

hashCode() Reurns an ineger hash code or he objec.

geClass() Reurns he runme class o he objec.

clone() Creaes and reurns a copy o he objec. (Used wih Cloneable inerace.)

nalize() Called by he garbage collecor when here are no more reerences o he objec.

Causes he curren hread o wai unl anoher hread invokes noy() or
wai()
noyAll() on he objec.

noy() Wakes up a single hread waing on he objec's monior.

noyAll() Wakes up all hreads waing on he objec's monior.

oSring():

• Reurns a sring represenaon o he objec.


• Deaul implemenaon: ClassName@HashCodeInHexadecimal.

• Typically overridden in cusom classes or meaningul oupu.

Example:

class Suden{

in rollno;

Sring name;

Sring ciy;

Suden(in rollno, Sring name, Sring ciy){

[Link]=rollno;

[Link]=name;

[Link]y=ciy;

public Sring oSring(){//overriding he oSring() mehod

reurn rollno+" "+name+" "+ciy;

public sac void main(Sring args[]){

Suden s1=new Suden(101,"Raj","lucknow");

Suden s2=new Suden(102,"Vijay","ghaziabad");

Sys[Link].prinln(s1);//compiler wries here s1.oSring()

Sys[Link].prinln(s2);//compiler wries here s2.oSring()

Oupu:

101 Raj lucknow

102 Vijay ghaziabad


Absracton in Java
Denion:

Absracon in Java is he process o hiding implemenaon deails and showing only essenal
eaures o an objec or a sysem. I ocuses on what an objec does insead o how i does i.
Absracon is achieved in Java hrough absrac classes and ineraces.

Key Characeriscs:

1. Purpose: Simplies complexiy by exposing only necessary unconaliy o he user.

2. Focus: Denes unconaliy raher han implemenaon.

3. Achieved Through:

o Absrac Classes.

o Ineraces.

Deails

1. Absrac Class:

• A class declared wih he absrac keyword.

• Can have boh absrac mehods (wihou implemenaon) and concree mehods (wih
implemenaon).

• Canno be insanaed direcly.

Synax:

absrac class AbsracClass {

// Absrac mehod (no body)

absrac void absracMehod();

// Concree mehod (has body)

void concreeMehod() {

Sys[Link].prinln("This is a concree mehod.");

Example:

absrac class Shape {


absrac void draw(); // Absrac mehod

void display() { // Concree mehod

Sys[Link].prinln("This is a shape.");

class Circle exends Shape {

@Override

void draw() {

Sys[Link].prinln("Drawing a circle.");

public class Main {

public sac void main(Sring[] args) {

Shape shape = new Circle(); // Polymorphism

[Link]();

[Link]();

Oupu:

Drawing a circle.

This is a shape.

2. Inerface:

• A blueprin or a class conaining only absrac mehods (unl Java 7).

• From Java 8 onwards, can also include:

o Deaul mehods (mehods wih implemenaon).

o Sac mehods.

• Suppors mulple inheriance by allowing a class o implemen mulple ineraces.


Synax:

inerace IneraceName {

void absracMehod(); // Absrac mehod

deaul void deaulMehod() {

Sys[Link].prinln("This is a deaul mehod.");

Example:

inerace Animal {

void sound(); // Absrac mehod

deaul void breahe() { // Deaul mehod

Sys[Link].prinln("Animals breahe oxygen.");

class Dog implemens Animal {

@Override

public void sound() {

Sys[Link].prinln("Dog barks.");

public class Main {

public sac void main(Sring[] args) {

Animal dog = new Dog();

[Link]();

[Link]he();

}
Oupu:

Dog barks.

Animals breahe oxygen.

Key Dierences Beween Absrac Classes and Inerfaces

Aspec Absrac Class Inerface

Declaraon Declared using he absrac keyword. Declared using he inerace keyword.

Can have boh absrac and concree Can have absrac, deaul, and sac
Mehods
mehods. mehods.

Can only have sac nal variables


Fields Can have insance variables.
(consans).

Mulple A class can exend only one absrac A class can implemen mulple
Inheriance class. ineraces.

Access Modiers Can have any access modier. Mehods are public by deaul.

Benes of Absracon:

1. Improved Code Mainainabiliy: By exposing only high-level unconaliy, changes in


implemenaon do no aec he users o he absracon.

2. Enhanced Securiy: Implemenaon deails are hidden rom he user.

3. Code Reusabiliy: Absrac classes and ineraces allow sharing common unconaliy among
relaed classes.

4. Scalabiliy: Easier o exend or modiy absraced sysems.

Encapsulaton in Java
Denion:

Encapsulaon is one o he undamenal principles o Objec-Oriened Programming (OOP). I reers


o he process o wrapping daa (elds) and mehods (uncons) ha operae on he daa ino a
single uni (class) and resricng direc access o he elds. Encapsulaon ensures ha he inernal
represenaon o an objec is hidden rom he ouside world and accessed only hrough well-dened
mehods.

Key Characeriscs:

1. Daa Hiding: Fields (daa) o a class are kep privae o preven unauhorized access.
2. Conrolled Access: Access o he elds is provided hrough public geter and seter mehods.

3. Improved Securiy: Only auhorized mehods can modiy he daa.

4. Increased Mainainabiliy: Code changes in one par o he class do no aec oher pars o
he applicaon.

Deails of Encapsulaon

Componens of Encapsulaon:

1. Privae Fields: Daa members o a class are declared as privae o resric access.

2. Public Mehods: Public geter and seter mehods are used o access and modiy privae
elds.

3. Validaon Logic: Seters can include validaon rules o ensure daa inegriy.

Code Implemenaon

Example 1: Basic Encapsulaon

class Suden {

privae Sring name; // Privae eld

privae in age; // Privae eld

// Geter or name

public Sring geName() {

reurn name;

// Seter or name

public void seName(Sring name) {

[Link] = name;

// Geter or age

public in geAge() {

reurn age;
}

// Seter or age wih validaon

public void seAge(in age) {

i (age > 0) {

[Link] = age;

} else {

Sys[Link].prinln("Age mus be posive.");

public class Main {

public sac void main(Sring[] args) {

Suden suden = new Suden();

suden.seName("Anil");

suden.seAge(20);

Sys[Link].prinln("Name: " + suden.geName());

Sys[Link].prinln("Age: " + suden.geAge());

Oupu:

Name: Anil

Age: 20
Inheriance in Java
Denion:

Inheriance is an Objec-Oriened Programming (OOP) concep where one class (child class or
subclass) can acquire he properes (elds) and behaviors (mehods) o anoher class (paren class
or superclass). I allows or code reuse and he creaon o a hierarchical relaonship beween
classes.

Key Characeriscs:

1. Code Reusabiliy: Enables a subclass o reuse he code rom a paren class.

2. Hierarchy: Creaes a relaonship beween general and specic classes.

3. Exensibiliy: Faciliaes he addion o new eaures o exisng classes.

4. Types of Inheriance:

o Single Inheriance.

o Mullevel Inheriance.

o Hierarchical Inheriance.

Use Cases:

1. Reusing exisng unconaliy in new classes.

2. Implemenng polymorphism (mehod overriding).

3. Creang a hierarchical srucure o relaed classes.

Deails

Synax:

class ParenClass {

// Paren class members

class ChildClass exends ParenClass {

// Child class members

Code Implemenaon
Example 1: Single Inheriance

class Animal {

void ea() {

Sys[Link].prinln("This animal eas ood.");

class Dog exends Animal {

void bark() {

Sys[Link].prinln("The dog barks.");

public class Main {

public sac void main(Sring[] args) {

Dog dog = new Dog();

[Link](); // Inheried mehod

[Link](); // Mehod o Dog class

Oupu:

This animal eas ood.

The dog barks.

Example 2: Mullevel Inheriance

class Animal {

void ea() {

Sys[Link].prinln("This animal eas ood.");

}
class Mammal exends Animal {

void walk() {

Sys[Link].prinln("This mammal walks.");

class Dog exends Mammal {

void bark() {

Sys[Link].prinln("The dog barks.");

public class Main {

public sac void main(Sring[] args) {

Dog dog = new Dog();

[Link](); // Inheried rom Animal

[Link](); // Inheried rom Mammal

[Link](); // Dened in Dog

Oupu:

This animal eas ood.

This mammal walks.

The dog barks.

Example 3: Hierarchical Inheriance

class Animal {

void ea() {

Sys[Link].prinln("This animal eas ood.");

}
class Dog exends Animal {

void bark() {

Sys[Link].prinln("The dog barks.");

class Ca exends Animal {

void meow() {

Sys[Link].prinln("The ca meows.");

public class Main {

public sac void main(Sring[] args) {

Dog dog = new Dog();

[Link]();

[Link]();

Ca ca = new Ca();

ca.ea();

ca.meow();

Oupu:

This animal eas ood.

The dog barks.

This animal eas ood.

The ca meows.

Types of Inheriance in Java:


1. Single Inheriance: A subclass inheris rom a single paren class.

2. Mullevel Inheriance: A class inheris rom a subclass, orming a chain.

3. Hierarchical Inheriance: Mulple subclasses inheri rom a single paren class.

Mehod Overriding in Inheriance

When a subclass provides a specic implemenaon o a mehod ha is already dened in is paren
class, i is known as mehod overriding. This is a key eaure o polymorphism.

Example:

java

Copy code

class Animal {

void sound() {

Sys[Link].prinln("Animal makes a sound.");

class Dog exends Animal {

@Override

void sound() {

Sys[Link].prinln("Dog barks.");

public class Main {

public sac void main(Sring[] args) {

Animal animal = new Dog(); // Polymorphism

[Link]();

Oupu:

Dog barks.
Access Specifiers in Java
Denion:

Access speciers in Java dene he visibiliy and accessibiliy o classes, mehods, and variables. They
deermine which pars o a program can access a parcular class member.

Java provides four ypes of access speciers:

1. Public

2. Privae

3. Proeced

4. Defaul (no keyword)

Types of Access Speciers

1. Public

• Denion: The public keyword makes a class, mehod, or eld accessible rom anywhere in
he program.

• Scope: Visible o all classes, even ouside he package.

• Use Case: When mehods or variables need o be accessible across all packages.

Example:

class PublicExample {

public void show() {

Sys[Link].prinln("This is a public mehod.");

public class Main {

public sac void main(Sring[] args) {

PublicExample obj = new PublicExample();

[Link](); // Accessible

Oupu:

This is a public mehod.


2. Privae

• Denion: The privae keyword resrics access o he class isel.

• Scope:

o Visible only wihin he class where i is dened.

o No accessible by subclasses or oher classes.

• Use Case: When daa or mehods should no be accessed direcly or securiy or
encapsulaon.

Example:

class PrivaeExample {

privae in number = 10;

privae void display() {

Sys[Link].prinln("This is a privae mehod.");

public void accessPrivae() {

display(); // Privae mehod accessed wihin he class

public class Main {

public sac void main(Sring[] args) {

PrivaeExample obj = new PrivaeExample();

[Link]e(); // Indirec access o he privae mehod

Oupu:

This is a privae mehod.

3. Proeced

• Denion: The proeced keyword allows access wihin he same package and o subclasses,
even i hey are in dieren packages.
• Scope:

o Accessible in he same package.

o Accessible in subclasses hrough inheriance (even in dieren packages).

• Use Case: When exending classes and allowing conrolled access o variables or mehods.

Example:

class Paren {

proeced void display() {

Sys[Link].prinln("This is a proeced mehod.");

class Child exends Paren {

void show() {

display(); // Proeced mehod accessed in subclass

public class Main {

public sac void main(Sring[] args) {

Child obj = new Child();

[Link]();

Oupu:

This is a proeced mehod.


Comparison Table

Access Specier Wihin Class Same Package Subclass (Dieren Package) Ouside Package

Public Yes Yes Yes Yes

Privae Yes No No No

Proeced Yes Yes Yes (i subclassed) No

Defaul Yes Yes No No

Key Poins:

1. Public: Mos open, accessible everywhere.

2. Privae: Mos resricve, visible only wihin he same class.

3. Proeced: Balanced access or inheriance and package-level use.

4. Defaul: Package-privae access, no visibiliy ouside he package.

Polymorphism in Java
Denion:

Polymorphism is one o he core conceps o Objec-Oriened Programming (OOP) ha allows
objecs o dieren classes o be reaed as objecs o a common superclass. The erm polymorphism
means "many shapes" and i allows a single acon o behave dierenly based on he objec ha i is
acng upon. In Java, polymorphism can be achieved in wo ways:

1. Compile-me Polymorphism (Mehod Overloading)

2. Runme Polymorphism (Mehod Overriding)

Types of Polymorphism:

1. Compile-me Polymorphism (Mehod Overloading):

• Denion: Mehod overloading occurs when wo or more mehods in he same class have
he same name bu dieren parameers (eiher in number or ype).

• How i works: The mehod is seleced a compile me based on he mehod signaure
(number, ype, and order o parameers).

• Use Case: Used o perorm similar operaons wih dieren ypes or numbers o argumens.
Example:

class Calculaor {

// Overloaded mehod wih wo ineger parameers

public in add(in a, in b) {

reurn a + b;

// Overloaded mehod wih hree ineger parameers

public in add(in a, in b, in c) {

reurn a + b + c;

public class Main {

public sac void main(Sring[] args) {

Calculaor calc = new Calculaor();

Sys[Link].prinln([Link](5, 10)); // Calls mehod wih wo parameers

Sys[Link].prinln([Link](5, 10, 15)); // Calls mehod wih hree parameers

Oupu:

15

30

2. Runme Polymorphism (Mehod Overriding):

• Denion: Mehod overriding occurs when a subclass provides a specic implemenaon or
a mehod ha is already dened in is superclass. The mehod is called a runme, and he
subclass mehod is invoked based on he ype o objec being reerred o, no he reerence
ype.

• How i works: The mehod is seleced a runme based on he objec's ype.

• Use Case: Used o implemen dynamic mehod dispach, especially when you wan o
change or exend he behavior o mehods in he superclass.
Example:

class Animal {

// Mehod in superclass

public void sound() {

Sys[Link].prinln("Animal makes a sound");

class Dog exends Animal {

// Overridden mehod in subclass

@Override

public void sound() {

Sys[Link].prinln("Dog barks");

class Ca exends Animal {

// Overridden mehod in subclass

@Override

public void sound() {

Sys[Link].prinln("Ca meows");

public class Main {

public sac void main(Sring[] args) {

Animal myAnimal = new Animal(); // Paren class reerence

Animal myDog = new Dog(); // Child class reerence

Animal myCa = new Ca(); // Child class reerence

[Link](); // Calls Animal's sound mehod


[Link](); // Calls Dog's overridden sound mehod

myCa.sound(); // Calls Ca's overridden sound mehod

Oupu:

Animal makes a sound

Dog barks

Ca meows

You might also like