SlideShare a Scribd company logo
1. A program that demonstrates the extends keyword in Java is given as
follows
Code:
class A {
int a = 9;
}
class B extends A {
int b = 4;
}
public class Demo {
public static void main(String args[]) {
B obj = new B();
System.out.println("Value of a is: " + obj.a);
System.out.println("Value of b is: " + obj.b);
}
}
Output: Value of a is: 9
Value of b is: 4
Practice Program-2 on Extend Keyword
class Animal {
// fields of the parent class
String name;
String sound;
int noOfLegs;
// default constructor of the parent class
public Animal (){}
// parameterized constructor of the parent class
public Animal (String name, String sound, int legs){
this.name = name;
this.sound = sound;
this.noOfLegs = legs;
}
// method of the parent class
public void display() {
System.out.println("My name is " + name);
System.out.println("My sound is " + sound);
System.out.println("My no. of legs is " + noOfLegs);
}
}
// inherit from Animal
class Dog extends Animal {
String color;
String breed;
// new method in subclass
public Dog(String name, String sound ,int legs, String
color, String breed){
super(name,sound,legs);
this.color = color;
this.breed = breed;
}
public void display() {
super.display();
System.out.println("My color is " + color);
System.out.println("My breed is " + breed);
}
}
public class Main {
public static void main(String[] args) {
// create an object of the subclass
Dog dog1 = new
Dog("Billy","Bark",4,"Brown","Labrador");
dog1.display();
System.out.println("------------------");
Dog dog2 = new Dog("Grace","Bark",4,"Black","Husky");
dog2.display();
System.out.println("------------------");
Dog dog3 = new Dog("Hugo","Bark",4,"Gray","Poodle");
dog3.display();
}
}
Output:
My name is Billy
My sound is Bark
My no. of legs is 4
My color is Brown
My breed is Labrador
------------------
My name is Grace
My sound is Bark
My no. of legs is 4
My color is Black
My breed is Husky
------------------
My name is Hugo
My sound is Bark
My no. of legs is 4
My color is Gray
My breed is Poodle
Program 3. Demonstrate execution of constructor in Single inheritance
/* Parent Class */
class ParentClass
{
/* Constructor */
ParentClass()
{
System.out.println("ParentClass constructor executed.");
}
}
/* Child Class */
class ChildClass extends ParentClass
{
/* Constructor */
ChildClass()
{
System.out.println("ChildClass constructor executed.");
}
}
public class OrderofExecution1
{
/* Driver Code */
public static void main(String ar[])
{
/* Create instance of ChildClass */
System.out.println("Order of constructor execution...");
new ChildClass();
}
}
Order of constructor execution...
ParentClass constructor executed.
ChildClass constructor executed.
Program 4. Demonstrate execution of constructor in multiple inheritances
class College
{
/* Constructor */
College()
{
System.out.println("College constructor executed");
}
}
class Department extends College
{
/* Constructor */
Department()
{
System.out.println("Department constructor executed");
}
}
class Student extends Department
{
/* Constructor */
Student()
{
System.out.println("Student constructor executed");
}
}
public class OrderofExecution2
{
/* Driver Code */
public static void main(String[] args)
{
/* Create instance of Student class */
System.out.println("Order of constructor execution in Multilevel inhe
ritance...");
new Student();
}
}
Output:
Order of constructor execution in multilevel inheritance...
College constructor executed
Department constructor executed
Student constructor executed
Program 5. Calling super class constructor using super keyword
/* Parent Class */
class ParentClass
{
int a;
ParentClass(int x)
{
a = x;
}
}
/* Child Class */
class ChildClass extends ParentClass
{
int b;
ChildClass(int x, int y)
{
/* Accessing ParentClass Constructor */
super(x);
b = y;
}
/* Method to show value of a and b */
void Show()
{
System.out.println("Value of a : "+a+"nValue of b : "+b);
}
}
public class OrderofExecution4
{
/* Driver Code */
public static void main(String ar[])
{
System.out.println("Order of constructor execution...");
ChildClass d = new ChildClass(79, 89);
d.Show();
}
}
Multilevel Hierarchy
In simple inheritance a subclass or derived class derives the properties from its parent
class, but in multilevel inheritance a subclass is derived from a derived class. One class
inherits only single class. Therefore, in multilevel inheritance, every time ladder
increases by one. The lower most class will have the properties of all the super classes’.
Example for multilevel hierarchy:
Program-1
class A {
void funcA() {
System.out.println("This is class A");
}
}
class B extends A {
void funcB() {
System.out.println("This is class B");
}
}
class C extends B {
void funcC() {
System.out.println("This is class C");
}
}
public class Demo {
public static void main(String args[]) {
C obj = new C();
obj.funcA();
obj.funcB();
obj.funcC();
}
}
Output
This is class A
This is class B
This is class C
Program-2
class student
{
int rollno;
String name;
student(int r, String n)
{
rollno = r;
name = n;
}
void dispdatas()
{
System.out.println("Rollno = " + rollno);
System.out.println("Name = " + name);
}
}
class marks extends student
{
int total;
marks(int r, String n, int t)
{
super(r,n); //call super class (student) constructor
total = t;
}
void dispdatam()
{
dispdatas(); // call dispdatap of student class
System.out.println("Total = " + total);
}
}
class percentage extends marks
{
int per;
percentage(int r, String n, int t, int p)
{
super(r,n,t); //call super class(marks) constructor
per = p;
}
void dispdatap()
{
dispdatam(); // call dispdatap of marks class
System.out.println("Percentage = " + per);
}
}
class Multi_Inhe
{
public static void main(String args[])
{
percentage stu = new percentage(102689, "RATHEESH", 350, 70); //call constructor
percentage
stu.dispdatap(); // call dispdatap of percentage class
}
}
Output:
Rollno = 102689
Name = RATHEESH
Total = 350
Percentage = 70
Program-3
class stu
{
int rollno;
String name;
stu(int r, String n)
{
rollno = r;
name = n;
}
void show()
{
System.out.println("Student Roll no - " + rollno);
System.out.println("Student Name - " + name);
}
}
class marks extends stu
{
int s1, s2, s3, s4, s5, sum1;
marks(int r, String n, int m1, int m2, int m3, int m4, int m5)
{
super(r,n);
s1 = m1;
s2 = m2;
s3 = m3;
s4 = m4;
s5 = m5;
}
void showmarks()
{
show();
sum1 = s1 + s2 + s3 + s4 + s5;
System.out.println("Total marks = " + sum1);
}
}
class percent extends marks
{
float per;
percent(int r, String n, int m1, int m2, int m3, int m4, int m5,
float p)
{
super(r,n,m1,m2,m3,m4,m5);
per=p;
}
void showper()
{
showmarks();
per = sum1 / 5;
System.out.println("Percentage = " + per);
}
}
class multiEg
{
public static void main(String args[])
{
percent p = new percent(101, "Nancy", 90, 75, 85, 90, 80, 0);
p.showper();
}
}

More Related Content

PDF
Program for Constructor Overloading and Inheritance.pdf
DOCX
OOP Lab Report.docx
PPTX
PDF
OOPs Concepts - Android Programming
PPT
7_-_Inheritance
DOCX
Unit3 java
PPTX
Java Inheritance
PPTX
Java oops features
Program for Constructor Overloading and Inheritance.pdf
OOP Lab Report.docx
OOPs Concepts - Android Programming
7_-_Inheritance
Unit3 java
Java Inheritance
Java oops features

Similar to Unit-3 Practice Programs-5.docx (20)

PDF
Second chapter-java
PDF
C h 04 oop_inheritance
PPTX
Inheritance Slides
PPTX
Inheritance and Polymorphism in java simple and clear
PPTX
Unit3 inheritance
PPT
Inheritance and-polymorphism
PPTX
Detailed_description_on_java_ppt_final.pptx
PPS
Inheritance chepter 7
PPTX
Unit3 part2-inheritance
PPTX
20.2 Java inheritance
PDF
Presentation 3.pdf
PPTX
Ch5 inheritance
PPTX
Lab 02bbnbnbbbb,nmn,mn,nnnklnlnlnknln.pptx
PPTX
Chap-3 Inheritance.pptx
PDF
java_inheritance.pdf
DOCX
Uta005
PPTX
Inheritance.pptx
PPT
Lecture 14 (inheritance basics)
PPT
RajLec10.ppt
DOCX
Assignment 7
Second chapter-java
C h 04 oop_inheritance
Inheritance Slides
Inheritance and Polymorphism in java simple and clear
Unit3 inheritance
Inheritance and-polymorphism
Detailed_description_on_java_ppt_final.pptx
Inheritance chepter 7
Unit3 part2-inheritance
20.2 Java inheritance
Presentation 3.pdf
Ch5 inheritance
Lab 02bbnbnbbbb,nmn,mn,nnnklnlnlnknln.pptx
Chap-3 Inheritance.pptx
java_inheritance.pdf
Uta005
Inheritance.pptx
Lecture 14 (inheritance basics)
RajLec10.ppt
Assignment 7
Ad

More from R.K.College of engg & Tech (15)

PDF
Module 5(Matplotlib and tkinter).pdf
PDF
Module 5(Numpy).pdf
PDF
Module 5(Pandas).pdf
PDF
Module IV_updated(old).pdf
PDF
PDF
Python_Module_2.pdf
PDF
Python_Module_1.pdf
PDF
Python for Data Analysis_ Data Wrangling with Pandas, Numpy, and Ipython ( PD...
DOCX
Creating Interface- Practice Program 6.docx
DOCX
Practice Program-9-Packages-Unit 4.docx
Module 5(Matplotlib and tkinter).pdf
Module 5(Numpy).pdf
Module 5(Pandas).pdf
Module IV_updated(old).pdf
Python_Module_2.pdf
Python_Module_1.pdf
Python for Data Analysis_ Data Wrangling with Pandas, Numpy, and Ipython ( PD...
Creating Interface- Practice Program 6.docx
Practice Program-9-Packages-Unit 4.docx
Ad

Recently uploaded (20)

PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPT
Project quality management in manufacturing
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
Welding lecture in detail for understanding
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Construction Project Organization Group 2.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
Sustainable Sites - Green Building Construction
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
web development for engineering and engineering
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Project quality management in manufacturing
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Operating System & Kernel Study Guide-1 - converted.pdf
Welding lecture in detail for understanding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
R24 SURVEYING LAB MANUAL for civil enggi
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Construction Project Organization Group 2.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Digital Logic Computer Design lecture notes
Sustainable Sites - Green Building Construction
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
web development for engineering and engineering

Unit-3 Practice Programs-5.docx

  • 1. 1. A program that demonstrates the extends keyword in Java is given as follows Code: class A { int a = 9; } class B extends A { int b = 4; } public class Demo { public static void main(String args[]) { B obj = new B(); System.out.println("Value of a is: " + obj.a); System.out.println("Value of b is: " + obj.b); } } Output: Value of a is: 9 Value of b is: 4 Practice Program-2 on Extend Keyword class Animal { // fields of the parent class String name; String sound; int noOfLegs; // default constructor of the parent class public Animal (){} // parameterized constructor of the parent class public Animal (String name, String sound, int legs){ this.name = name; this.sound = sound; this.noOfLegs = legs;
  • 2. } // method of the parent class public void display() { System.out.println("My name is " + name); System.out.println("My sound is " + sound); System.out.println("My no. of legs is " + noOfLegs); } } // inherit from Animal class Dog extends Animal { String color; String breed; // new method in subclass public Dog(String name, String sound ,int legs, String color, String breed){ super(name,sound,legs); this.color = color; this.breed = breed; } public void display() { super.display(); System.out.println("My color is " + color); System.out.println("My breed is " + breed); } } public class Main { public static void main(String[] args) { // create an object of the subclass Dog dog1 = new Dog("Billy","Bark",4,"Brown","Labrador"); dog1.display(); System.out.println("------------------"); Dog dog2 = new Dog("Grace","Bark",4,"Black","Husky");
  • 3. dog2.display(); System.out.println("------------------"); Dog dog3 = new Dog("Hugo","Bark",4,"Gray","Poodle"); dog3.display(); } } Output: My name is Billy My sound is Bark My no. of legs is 4 My color is Brown My breed is Labrador ------------------ My name is Grace My sound is Bark My no. of legs is 4 My color is Black My breed is Husky ------------------ My name is Hugo My sound is Bark My no. of legs is 4 My color is Gray My breed is Poodle
  • 4. Program 3. Demonstrate execution of constructor in Single inheritance /* Parent Class */ class ParentClass { /* Constructor */ ParentClass() { System.out.println("ParentClass constructor executed."); } } /* Child Class */ class ChildClass extends ParentClass { /* Constructor */ ChildClass() { System.out.println("ChildClass constructor executed."); } } public class OrderofExecution1 { /* Driver Code */ public static void main(String ar[]) { /* Create instance of ChildClass */ System.out.println("Order of constructor execution..."); new ChildClass(); } }
  • 5. Order of constructor execution... ParentClass constructor executed. ChildClass constructor executed. Program 4. Demonstrate execution of constructor in multiple inheritances class College { /* Constructor */ College() { System.out.println("College constructor executed"); } } class Department extends College { /* Constructor */ Department() { System.out.println("Department constructor executed"); } } class Student extends Department { /* Constructor */ Student() { System.out.println("Student constructor executed"); }
  • 6. } public class OrderofExecution2 { /* Driver Code */ public static void main(String[] args) { /* Create instance of Student class */ System.out.println("Order of constructor execution in Multilevel inhe ritance..."); new Student(); } } Output: Order of constructor execution in multilevel inheritance... College constructor executed Department constructor executed Student constructor executed Program 5. Calling super class constructor using super keyword /* Parent Class */ class ParentClass { int a; ParentClass(int x) { a = x; } }
  • 7. /* Child Class */ class ChildClass extends ParentClass { int b; ChildClass(int x, int y) { /* Accessing ParentClass Constructor */ super(x); b = y; } /* Method to show value of a and b */ void Show() { System.out.println("Value of a : "+a+"nValue of b : "+b); } } public class OrderofExecution4 { /* Driver Code */ public static void main(String ar[]) { System.out.println("Order of constructor execution..."); ChildClass d = new ChildClass(79, 89); d.Show(); } }
  • 8. Multilevel Hierarchy In simple inheritance a subclass or derived class derives the properties from its parent class, but in multilevel inheritance a subclass is derived from a derived class. One class inherits only single class. Therefore, in multilevel inheritance, every time ladder increases by one. The lower most class will have the properties of all the super classes’. Example for multilevel hierarchy: Program-1 class A { void funcA() { System.out.println("This is class A"); } } class B extends A { void funcB() { System.out.println("This is class B"); } } class C extends B { void funcC() { System.out.println("This is class C"); } } public class Demo { public static void main(String args[]) { C obj = new C(); obj.funcA(); obj.funcB(); obj.funcC(); } } Output
  • 9. This is class A This is class B This is class C Program-2 class student { int rollno; String name; student(int r, String n) { rollno = r; name = n; } void dispdatas() { System.out.println("Rollno = " + rollno); System.out.println("Name = " + name); } } class marks extends student { int total; marks(int r, String n, int t) { super(r,n); //call super class (student) constructor total = t; } void dispdatam() { dispdatas(); // call dispdatap of student class System.out.println("Total = " + total); } } class percentage extends marks { int per;
  • 10. percentage(int r, String n, int t, int p) { super(r,n,t); //call super class(marks) constructor per = p; } void dispdatap() { dispdatam(); // call dispdatap of marks class System.out.println("Percentage = " + per); } } class Multi_Inhe { public static void main(String args[]) { percentage stu = new percentage(102689, "RATHEESH", 350, 70); //call constructor percentage stu.dispdatap(); // call dispdatap of percentage class } } Output: Rollno = 102689 Name = RATHEESH Total = 350 Percentage = 70 Program-3 class stu { int rollno; String name; stu(int r, String n) { rollno = r; name = n; } void show() { System.out.println("Student Roll no - " + rollno); System.out.println("Student Name - " + name); } } class marks extends stu
  • 11. { int s1, s2, s3, s4, s5, sum1; marks(int r, String n, int m1, int m2, int m3, int m4, int m5) { super(r,n); s1 = m1; s2 = m2; s3 = m3; s4 = m4; s5 = m5; } void showmarks() { show(); sum1 = s1 + s2 + s3 + s4 + s5; System.out.println("Total marks = " + sum1); } } class percent extends marks { float per; percent(int r, String n, int m1, int m2, int m3, int m4, int m5, float p) { super(r,n,m1,m2,m3,m4,m5); per=p; } void showper() { showmarks(); per = sum1 / 5; System.out.println("Percentage = " + per); } } class multiEg { public static void main(String args[]) { percent p = new percent(101, "Nancy", 90, 75, 85, 90, 80, 0); p.showper(); } }