SlideShare a Scribd company logo
INHERITANCE
AUTHOR:- AHSAN RAJA
INHERITANCE
A mechanism wherein a new class is derived from an existing class.
In Java, classes may inherit or acquire the properties and methods of other classes.
A class derived from another class is called a subclass, whereas the class from which a subclass is derived is ca
a superclass. A subclass can have only one superclass, whereas a superclass may have one or more subclasses
class Super{
.....
….
}
class Sub extends Super{
.....
…..
}
 Permits sharing and accessing properties form one to another class.
 To establish this relation java uses “EXTEND” keyword.
 Example:
class Employee{
float salary=40000;
}
class Programmer extends Employee{
int bonus=10000;
public static void main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
}
}
ADVANTAGES OF INHERITANCE
 Minimize the amount of duplicate code in an application by sharing common code amongst
several subclasses.
 It also make application code more flexible to change because classes that inherit from a
common superclass can
be used interchangeably.
 Reusability -- facility to use public methods of base class without rewriting the same.
 Data hiding -- base class can decide to keep some data private so that it cannot be altered by
the derived class.
 Overriding--With inheritance, we will be able to override the methods of the base class so that
meaningful
implementation of the base class method can be designed in the derived class.
IS-A & HAS-A Relationship
• If a class is child of another class then we have is a relation
• If an object contain an instance of a class this is called has a relation.
HAS-A RELATIONSHIP
package relationships;
class Car {
private String colour;
private int maxSpeed;
public void carInfo(){
System.out.println("Car Colour= "+colour + " Max Speed= " + maxSpeed);
}
public void setColor(String colour) {
this.colour = colour;
}
public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}
}
IS-A RELATIONSHIP
class Audi extends Car
{
//Audi extends Car and thus inherits all methods from Car (except final and
static)
public void AudiStartDemo(){
Engine AudiEngine = new Engine();
AudiEngine.start();
}
}
SPECIALIZATION AND GENERALIZATION
o The process of extracting common characteristics from two or more
classes and combining them into a generalized superclass, is called
Generalization. The common characteristics can be attributes or
methods. Generalization is represented by a triangle followed by a line.
o Specialization is the reverse process of Generalization means creating
new sub classes from an existing class.
 There is no limit to the number of subclasses a class can
have.
 There is no limit to the depth of the class tree.
 Subclasses are more specific and have more functionality.
 Super classes capture generic functionality common across
many types of objects.
SUB CLASS CONSTRUCTOR
a subclass constructor is used to construct the inheritance instance
variable of both the subclass and super class
the subclass constructor uses the keyword super to invoke the
constructor method of the super class.
TYPES OF INHERITANCE
A. SINGLE INHERITANCE
B. MULTILEVEL INHERITANCE
C. MULTIPLE INHERITANCE
D. HIERARCHICAL INHERITANCE
E. HYBRID INHERITANCE
TYPE OF INHERITANCE
 Single Inheritance
When a class extends another class(Only one class) then we call it
as Single inheritance.
public class ClassA
{
public void dispA(){
System.out.println("disp() method of ClassA");
}
}
public class ClassB extends ClassA {
public void dispB() {
System.out.println("disp() method of ClassB");
}
public static void main(String args[]) {
ClassB b = new ClassB();
b.dispA();
b.dispB();
}
}
 Multilevel Inheritance
In Multilevel Inheritance a derived class will be inheriting a parent class and as well
as the derived class act as the parent class to other class.
public class ClassA {
public void dispA() {
System.out.println("disp() method of ClassA");
} }
public class ClassB extends ClassA {
public void dispB() {
System.out.println("disp() method of ClassB");
} }
public class ClassC extends ClassB {
public void dispC() {
System.out.println("disp() method of ClassC");
}
public static void main(String args[]) {
ClassC c = new ClassC();
c.dispA();
c.dispB();
c.dispC();
}
}
 Hierarchical Inheritance
In this inheritance multiple classes inherits from a single class i.e. there is one super
class and multiple sub classes.
public class ClassA {
public void dispA() {
System.out.println("disp() method of ClassA"); } }
public class ClassB extends ClassA {
public void dispB() {
System.out.println("disp() method of ClassB"); } }
public class ClassC extends ClassA {
public void dispC() {
System.out.println("disp() method of ClassC"); } }
public class ClassD extends ClassA {
public void dispD() {
System.out.println("disp() method of ClassD"); } }
public class HierarchicalInheritanceTest {
public static void main(String args[]) {
ClassB b = new ClassB();
b.dispB();
b.dispA();
ClassC c = new ClassC();
c.dispC();
c.dispA();
ClassD d = new ClassD();
d.dispD();
d.dispA(); } }
Access Control
Access control keywords define which classes can access classes, methods, and
members
Modifiers Class Package Subclass World
Public
Protected
Private
SUPER KEYWORD
 It is used inside a sub-class method definition to call a method defined in
the super class. Private methods of the super-class cannot be called.
Only public and protected methods can be called by the super keyword.
 It is also used by class constructors to invoke constructors of its parent
class.
USE OF SUPER KEYWORD
it calls the superclass constructor
inheritance syntax :- super( parameter list);
access the member of the super class
syntax:- super. member variable;
class Super_class{
int num = 20;
public void display(){
System.out.println("This is the display method of superclass");
} }
public class Sub_class extends Super_class {
int num = 10;
public void display(){
System.out.println("This is the display method of subclass");
}
public void my_method(){
Sub_class sub = new Sub_class();
sub.display();
super.display();
System.out.println("value of the variable named num in sub class:"+ sub.num);
System.out.println("value of the variable named num in super class:"+ super.num);
}
public static void main(String args[]){
Sub_class obj = new Sub_class();
obj.my_method();
} }
Invoking Superclass constructor
If a class is inheriting the properties of another class, the subclass automatically
acquires the default constructor of the super class. But if we want to call a
parametrized constructor of the super class, you need to use the super keyword
class Superclass{
int age;
Superclass(int age){
this.age = age; }
public void getAge(){
System.out.println("The value of the variable named age in super class is: "
+age); } }
public class Subclass extends Superclass {
Subclass(int age){
super(age); }
public static void main(String args[]){
Subclass s = new Subclass(24);
s.getAge();
} }
Method overriding
Declaring a method in subclass which is already present in parent class is known as
method overriding.
class Human{
public void eat() {
System.out.println("Human is eating"); }
}
class Boy extends Human{
public void eat(){ // @overriding
System.out.println("Boy is eating"); }
public static void main( String args[]) {
Boy obj = new Boy();
obj.eat();
}
}
Rules of method overriding in Java
 Argument list: The argument list of overriding method must be same as that of the
method in parent class. The data types of the arguments and their sequence should
be maintained as it is in the overriding method.
 Access Modifier: The Access Modifier of the overriding method (method of
subclass) cannot be more restrictive than the overridden method of parent class.
 Private, Static and final methods cannot be overridden as they are local to the
class. However static methods can be re-declared in the sub class, in this case the
sub-class method would act differently and will have nothing to do with the same
static method of parent class.
 Must be IS-A relationship (inheritance).
ANOTHER EXAMPLEOF OVERRIDING
class Animal{
public void move(){
system.out.println("animals can move"); } }
class Dog extends Animal{
public void move(){
system.out.println("dogs can walk and run"); } }
public class TestDog{
static void main(string args[])
{ Animal a = new Animal(); // animal reference and object
Animal b = new Dog(); // animal reference but dog object
a.move();// runs the method in animal class
b.move();//runs the method in dog class } }
PROGRAMS OF Hierachical INHERRITANCE
package office;
import java.util.Scanner;
class parent
{ int code;
String name;
String address;
int phonenum;
void enterdata()
{
Scanner dc=new Scanner(System.in);//use to
take string values
Scanner in=new Scanner(System.in);//use
to take integer value
System.out.println("Enter Code=");
code=in.nextInt();
System.out.println("Enter Name=");
name=dc.nextLine();
System.out.println("Enter Address=");
address=dc.nextLine();
System.out.println("Enter Phone-
Number=");
phonenum=in.nextInt();
}
void display()
{
System.out.println("Code="+code);
System.out.println("Name="+name);
System.out.println("Address="+address);
System.out.println("phonenum="+phonenum);
}
}
class Typest extends parent
{
int pay;
void enterT()
{
enterdata();
Scanner in=new Scanner(System.in);
System.out.println("Enterdata");
pay=in.nextInt();
}
}
class Officer extends parent
{
int pay;
void enterO()
{
enterdata();
Scanner in=new Scanner(System.in);
System.out.println("Enterdata");
pay=in.nextInt();
display();
System.out.println("Basic pay="+pay);
}
public class Office {
public static void main(String[] args) {
Typest T=new Typest();
T.enterT();
Officer O=new Officer();
O.enterO();
}
}
Static keyword in java
The static keyword in java is used for memory management mainly.
 the static can be;
Variable
Function/method
It makes your program memory efficient
Static :
Static keyword does not require obj it can be called without obj also the value of static
keyword remains same.
Final :
Final keyword stops overriding of any method or variable …
 Class Student8{
Int rollno;
String name;
Static String college=“ITS”;
Student8(int r, String n){
rollno=r;
name=n;}
Void display (){
System.out.println(rollno+” “+name” “+college);}
public static void main(String[ ] args){
student8 s1=new student8(228,”mahnoor”);
student8 s2=new student8(051,”sana”);
s1.display();
s2.display();
}
Why java main method is static ?
Because object is not required to call static method if it were non-
static method, JVM create object first then call main() method
that will lead the problem of extra memory allocation.
FINAL KEYWORD IN JAVA
IT is used in java to restrict the user. It can be use in many
contexts.
Variable
Method
Class
JAVA FINAL KEYWORD
 Stop change value
 Stop method overriding
 Stop inheritance
class Bike{
final void run(){
System.out.println(“running”);}
class Honda extends Bike{
void run(){
System.out.println(“running safetly”);}
Public static void main(String[ ] args){
Honda honda= new Honda();
Honda.run();
}
}
Inheritance Slides

More Related Content

PDF
Java unit2
PPTX
Java inheritance
PPTX
Dynamic method dispatch
PPT
Java Programming - Inheritance
PPTX
Inheritance
PDF
javainheritance
PPS
Inheritance chepter 7
Java unit2
Java inheritance
Dynamic method dispatch
Java Programming - Inheritance
Inheritance
javainheritance
Inheritance chepter 7

What's hot (20)

PDF
itft-Inheritance in java
PPTX
Inheritance
DOCX
JAVA Notes - All major concepts covered with examples
PPTX
Ppt on this and super keyword
PPT
Inheritance polymorphism-in-java
PPSX
Seminar on java
PDF
java-06inheritance
PPTX
Inheritance in Java
PPT
Inheritance in java
PPTX
Java Inheritance
PDF
C h 04 oop_inheritance
PPTX
Inheritance In Java
DOCX
Unit3 java
PPTX
encapsulation, inheritance, overriding, overloading
PPT
Java: Inheritance
PPTX
Inheritance in java
PDF
Inheritance In Java
PPT
Java inheritance
PPT
L7 inheritance
PPS
Introduction to class in java
itft-Inheritance in java
Inheritance
JAVA Notes - All major concepts covered with examples
Ppt on this and super keyword
Inheritance polymorphism-in-java
Seminar on java
java-06inheritance
Inheritance in Java
Inheritance in java
Java Inheritance
C h 04 oop_inheritance
Inheritance In Java
Unit3 java
encapsulation, inheritance, overriding, overloading
Java: Inheritance
Inheritance in java
Inheritance In Java
Java inheritance
L7 inheritance
Introduction to class in java
Ad

Viewers also liked (12)

PPTX
Genesis rodiguez
PDF
Christopher Odhiambo Joseph Operating Without Cultural And Art Education Policy
PDF
Sidur Messiânico Talmidei Yeshua HaMashiach (Em Hebreu)
PDF
MembershipCertificate
PPTX
Jayfox
PDF
Bedouin question with prophet
PPTX
Universidad central del ecuador investigación científica
DOCX
Morocco in Five Thumbnails
DOCX
Account Receivable QuestionsZ
PPTX
Aula a constituição da disciplina escolar ciências
PPT
Inventories and the Cost of Goods Sold
PPTX
Los artifices de la sociologia
Genesis rodiguez
Christopher Odhiambo Joseph Operating Without Cultural And Art Education Policy
Sidur Messiânico Talmidei Yeshua HaMashiach (Em Hebreu)
MembershipCertificate
Jayfox
Bedouin question with prophet
Universidad central del ecuador investigación científica
Morocco in Five Thumbnails
Account Receivable QuestionsZ
Aula a constituição da disciplina escolar ciências
Inventories and the Cost of Goods Sold
Los artifices de la sociologia
Ad

Similar to Inheritance Slides (20)

PPTX
Unit3 inheritance
PPTX
Unit3 part2-inheritance
PPTX
PPT
RajLec10.ppt
PPTX
Chap-3 Inheritance.pptx
PPTX
Ch5 inheritance
PPTX
Inheritance ppt
PPTX
Inheritance in oop
PPTX
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
PDF
Java programming -Object-Oriented Thinking- Inheritance
PPTX
Inheritance and Polymorphism Java
PPTX
Modules 333333333³3444444444444444444.pptx
PPTX
Inheritance in Java is a mechanism in which one object acquires all the prope...
PPTX
OOPS_Unit2.inheritance and interface objected oriented programming
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
PPTX
Inheritance in java
PPTX
Chapter 8.2
PPTX
Lec 1.6 Object Oriented Programming
PPTX
PPT
Inheritance & Polymorphism - 1
Unit3 inheritance
Unit3 part2-inheritance
RajLec10.ppt
Chap-3 Inheritance.pptx
Ch5 inheritance
Inheritance ppt
Inheritance in oop
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
Java programming -Object-Oriented Thinking- Inheritance
Inheritance and Polymorphism Java
Modules 333333333³3444444444444444444.pptx
Inheritance in Java is a mechanism in which one object acquires all the prope...
OOPS_Unit2.inheritance and interface objected oriented programming
‏‏‏‏‏‏oop lecture objectives will come.pdf
Inheritance in java
Chapter 8.2
Lec 1.6 Object Oriented Programming
Inheritance & Polymorphism - 1

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PDF
01-Introduction-to-Information-Management.pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Lesson notes of climatology university.
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Yogi Goddess Pres Conference Studio Updates
PPTX
UNIT III MENTAL HEALTH NURSING ASSESSMENT
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PPTX
Cell Types and Its function , kingdom of life
PPTX
master seminar digital applications in india
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx
Anesthesia in Laparoscopic Surgery in India
01-Introduction-to-Information-Management.pdf
What if we spent less time fighting change, and more time building what’s rig...
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Lesson notes of climatology university.
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Yogi Goddess Pres Conference Studio Updates
UNIT III MENTAL HEALTH NURSING ASSESSMENT
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
ChatGPT for Dummies - Pam Baker Ccesa007.pdf
LDMMIA Reiki Yoga Finals Review Spring Summer
Cell Types and Its function , kingdom of life
master seminar digital applications in india
Chinmaya Tiranga quiz Grand Finale.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Radiologic_Anatomy_of_the_Brachial_plexus [final].pptx

Inheritance Slides

  • 2. INHERITANCE A mechanism wherein a new class is derived from an existing class. In Java, classes may inherit or acquire the properties and methods of other classes. A class derived from another class is called a subclass, whereas the class from which a subclass is derived is ca a superclass. A subclass can have only one superclass, whereas a superclass may have one or more subclasses class Super{ ..... …. } class Sub extends Super{ ..... ….. }
  • 3.  Permits sharing and accessing properties form one to another class.  To establish this relation java uses “EXTEND” keyword.  Example: class Employee{ float salary=40000; } class Programmer extends Employee{ int bonus=10000; public static void main(String args[]){ Programmer p=new Programmer(); System.out.println("Programmer salary is:"+p.salary); System.out.println("Bonus of Programmer is:"+p.bonus); } }
  • 4. ADVANTAGES OF INHERITANCE  Minimize the amount of duplicate code in an application by sharing common code amongst several subclasses.  It also make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably.  Reusability -- facility to use public methods of base class without rewriting the same.  Data hiding -- base class can decide to keep some data private so that it cannot be altered by the derived class.  Overriding--With inheritance, we will be able to override the methods of the base class so that meaningful implementation of the base class method can be designed in the derived class.
  • 5. IS-A & HAS-A Relationship • If a class is child of another class then we have is a relation • If an object contain an instance of a class this is called has a relation.
  • 6. HAS-A RELATIONSHIP package relationships; class Car { private String colour; private int maxSpeed; public void carInfo(){ System.out.println("Car Colour= "+colour + " Max Speed= " + maxSpeed); } public void setColor(String colour) { this.colour = colour; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } }
  • 7. IS-A RELATIONSHIP class Audi extends Car { //Audi extends Car and thus inherits all methods from Car (except final and static) public void AudiStartDemo(){ Engine AudiEngine = new Engine(); AudiEngine.start(); } }
  • 8. SPECIALIZATION AND GENERALIZATION o The process of extracting common characteristics from two or more classes and combining them into a generalized superclass, is called Generalization. The common characteristics can be attributes or methods. Generalization is represented by a triangle followed by a line. o Specialization is the reverse process of Generalization means creating new sub classes from an existing class.
  • 9.  There is no limit to the number of subclasses a class can have.  There is no limit to the depth of the class tree.  Subclasses are more specific and have more functionality.  Super classes capture generic functionality common across many types of objects.
  • 10. SUB CLASS CONSTRUCTOR a subclass constructor is used to construct the inheritance instance variable of both the subclass and super class the subclass constructor uses the keyword super to invoke the constructor method of the super class.
  • 11. TYPES OF INHERITANCE A. SINGLE INHERITANCE B. MULTILEVEL INHERITANCE C. MULTIPLE INHERITANCE D. HIERARCHICAL INHERITANCE E. HYBRID INHERITANCE
  • 12. TYPE OF INHERITANCE  Single Inheritance When a class extends another class(Only one class) then we call it as Single inheritance.
  • 13. public class ClassA { public void dispA(){ System.out.println("disp() method of ClassA"); } } public class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } public static void main(String args[]) { ClassB b = new ClassB(); b.dispA(); b.dispB(); } }
  • 14.  Multilevel Inheritance In Multilevel Inheritance a derived class will be inheriting a parent class and as well as the derived class act as the parent class to other class.
  • 15. public class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } public class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } } public class ClassC extends ClassB { public void dispC() { System.out.println("disp() method of ClassC"); } public static void main(String args[]) { ClassC c = new ClassC(); c.dispA(); c.dispB(); c.dispC(); } }
  • 16.  Hierarchical Inheritance In this inheritance multiple classes inherits from a single class i.e. there is one super class and multiple sub classes.
  • 17. public class ClassA { public void dispA() { System.out.println("disp() method of ClassA"); } } public class ClassB extends ClassA { public void dispB() { System.out.println("disp() method of ClassB"); } } public class ClassC extends ClassA { public void dispC() { System.out.println("disp() method of ClassC"); } } public class ClassD extends ClassA { public void dispD() { System.out.println("disp() method of ClassD"); } } public class HierarchicalInheritanceTest { public static void main(String args[]) { ClassB b = new ClassB(); b.dispB(); b.dispA(); ClassC c = new ClassC(); c.dispC(); c.dispA(); ClassD d = new ClassD(); d.dispD(); d.dispA(); } }
  • 18. Access Control Access control keywords define which classes can access classes, methods, and members Modifiers Class Package Subclass World Public Protected Private
  • 19. SUPER KEYWORD  It is used inside a sub-class method definition to call a method defined in the super class. Private methods of the super-class cannot be called. Only public and protected methods can be called by the super keyword.  It is also used by class constructors to invoke constructors of its parent class.
  • 20. USE OF SUPER KEYWORD it calls the superclass constructor inheritance syntax :- super( parameter list); access the member of the super class syntax:- super. member variable;
  • 21. class Super_class{ int num = 20; public void display(){ System.out.println("This is the display method of superclass"); } } public class Sub_class extends Super_class { int num = 10; public void display(){ System.out.println("This is the display method of subclass"); } public void my_method(){ Sub_class sub = new Sub_class(); sub.display(); super.display(); System.out.println("value of the variable named num in sub class:"+ sub.num); System.out.println("value of the variable named num in super class:"+ super.num); } public static void main(String args[]){ Sub_class obj = new Sub_class(); obj.my_method(); } }
  • 22. Invoking Superclass constructor If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the super class. But if we want to call a parametrized constructor of the super class, you need to use the super keyword class Superclass{ int age; Superclass(int age){ this.age = age; } public void getAge(){ System.out.println("The value of the variable named age in super class is: " +age); } } public class Subclass extends Superclass { Subclass(int age){ super(age); } public static void main(String args[]){ Subclass s = new Subclass(24); s.getAge(); } }
  • 23. Method overriding Declaring a method in subclass which is already present in parent class is known as method overriding. class Human{ public void eat() { System.out.println("Human is eating"); } } class Boy extends Human{ public void eat(){ // @overriding System.out.println("Boy is eating"); } public static void main( String args[]) { Boy obj = new Boy(); obj.eat(); } }
  • 24. Rules of method overriding in Java  Argument list: The argument list of overriding method must be same as that of the method in parent class. The data types of the arguments and their sequence should be maintained as it is in the overriding method.  Access Modifier: The Access Modifier of the overriding method (method of subclass) cannot be more restrictive than the overridden method of parent class.  Private, Static and final methods cannot be overridden as they are local to the class. However static methods can be re-declared in the sub class, in this case the sub-class method would act differently and will have nothing to do with the same static method of parent class.  Must be IS-A relationship (inheritance).
  • 25. ANOTHER EXAMPLEOF OVERRIDING class Animal{ public void move(){ system.out.println("animals can move"); } } class Dog extends Animal{ public void move(){ system.out.println("dogs can walk and run"); } } public class TestDog{ static void main(string args[]) { Animal a = new Animal(); // animal reference and object Animal b = new Dog(); // animal reference but dog object a.move();// runs the method in animal class b.move();//runs the method in dog class } }
  • 26. PROGRAMS OF Hierachical INHERRITANCE package office; import java.util.Scanner; class parent { int code; String name; String address; int phonenum; void enterdata() { Scanner dc=new Scanner(System.in);//use to take string values Scanner in=new Scanner(System.in);//use to take integer value System.out.println("Enter Code="); code=in.nextInt(); System.out.println("Enter Name="); name=dc.nextLine(); System.out.println("Enter Address="); address=dc.nextLine(); System.out.println("Enter Phone- Number="); phonenum=in.nextInt(); } void display() { System.out.println("Code="+code); System.out.println("Name="+name); System.out.println("Address="+address); System.out.println("phonenum="+phonenum); } } class Typest extends parent { int pay; void enterT() { enterdata(); Scanner in=new Scanner(System.in); System.out.println("Enterdata"); pay=in.nextInt(); } } class Officer extends parent { int pay; void enterO() { enterdata(); Scanner in=new Scanner(System.in); System.out.println("Enterdata"); pay=in.nextInt(); display(); System.out.println("Basic pay="+pay); } public class Office { public static void main(String[] args) { Typest T=new Typest(); T.enterT(); Officer O=new Officer(); O.enterO(); } }
  • 27. Static keyword in java The static keyword in java is used for memory management mainly.  the static can be; Variable Function/method It makes your program memory efficient Static : Static keyword does not require obj it can be called without obj also the value of static keyword remains same. Final : Final keyword stops overriding of any method or variable …
  • 28.  Class Student8{ Int rollno; String name; Static String college=“ITS”; Student8(int r, String n){ rollno=r; name=n;} Void display (){ System.out.println(rollno+” “+name” “+college);} public static void main(String[ ] args){ student8 s1=new student8(228,”mahnoor”); student8 s2=new student8(051,”sana”); s1.display(); s2.display(); }
  • 29. Why java main method is static ? Because object is not required to call static method if it were non- static method, JVM create object first then call main() method that will lead the problem of extra memory allocation. FINAL KEYWORD IN JAVA IT is used in java to restrict the user. It can be use in many contexts. Variable Method Class
  • 30. JAVA FINAL KEYWORD  Stop change value  Stop method overriding  Stop inheritance class Bike{ final void run(){ System.out.println(“running”);} class Honda extends Bike{ void run(){ System.out.println(“running safetly”);} Public static void main(String[ ] args){ Honda honda= new Honda(); Honda.run(); } }