SlideShare a Scribd company logo
1 of 23
Ahmed - 1 . Inheritance
Polash - 2 . Polymorphism
Aman - 3 . Overloading
- 4 . Overriding
Mustafiz - 5 . Construction
- 6 . Encapsulation
Galib - 7 . Diff between method n construction
- 8 . Abstract
Welcome to our presentation
INTRODUCTION TO INHERITANCE
• Java classes are used in several ways.This is basically
by creating new classes,resuing the properties
existing one.
• The deriving the new class from an old one is called
inheritance .
• The old class is known as base class or super class.
• The class who inherit the properties of the base class
is called derived class or sub class.
TYPES OF INHERITANCE
• Single inheritance(one super class)
• Multiple inheritance
• Multilevel inheritance
• Hierarchical inheritance
DEFINE SUBCLASS
• Class sub-classname extends super-classname
• {
• variable declaration ;// sub class variables
• method declaration ;// sub class method
• }
• The keyword extends signifies that the properties of
the super class are extended to the sub class.
EXAMPLE OF SINGLE INHERITANCE
• Class sum //Super class
• {
• int a=10;
• int b=20;
• void show()
• {
• System.out.println(“value of a :-” +a);
• System.out.println(“value of a :-” +b);
• }
• }
• Class sum2 extends sum //base class
• {
• public static void main(String args[])
• {
• Sum2 obj =new sum2();
• Obj.show1();
}
}
Polymorphism is the ability of an object to
take on many forms. The most common use
of polymorphism in OOP occurs when a
parent class reference is used to refer to a
child class object. Any Java object that can
pass more than one IS-A test is considered
to be polymorphic. Any Java object that can
pass more than one IS-A test is considered
to be polymorphic. In Java, all Java objects
are polymorphic since any object will pass
the IS-A test for their own type and for the
class Object.
Polymorphism
Public class Dog extends Animal {
Public void make sound () {
System.out.println(“Woof !”);
}
Public void make sound (Boolean
injured) {
If (injured){
System.out.println(“Whimper”);
}
}
Polymorphism sample code
Definition: Overloading occurs when two or more
methods in one class have the same
method name but different parameters.
Overloading
Definition:
Overriding means having two methods with the same
method name and parameters. One of the methods is in
the parent class and the other is in the child class.
Overriding allows a child class to provide a specific
implementation of a method that is already provided its
parent class.
 In the example above, the dog variable is declared
to be a Dog. During compile time, the compiler
checks if the Dog class has the bark() method. As
long as the Dog class has the bark() method, the
code compilers. At run-time, a Hound is created
and assigned to dog. The JVM knows that dog is
referring to the object of Hound, so it calls
the bark() method of Hound. This is called
Dynamic Polymorphism
Constructor
A constructor in Java is a block of code similar to a method
that’s called when an instance of an object is created .
Constructor are 2 type
1 . Default constructor (no-arg
constructor)
2 . Parameterized constructor
Default Parameterized
Student obj = new Student ( ) Student obj = new Student ( name )
Difference between default and parameterized constructor
Public class StudentResult {
private string Full_Name ;
private string Exam_Name ;
private string Exam_Grade ;
StudentResult( String name , String grade ) {
Full_Name = name ;
Exam_Grade = grade ;
}
Constructor Sample Code
Encapsulation is one of the four fundamental OOP
concepts. The other three are inheritance,
polymorphism, and abstraction. Encapsulation in Java is
a mechanism of wrapping the data (variables) and code
acting on the data (methods) together as a single unit. ...
Declare the variables of a class as private.
To achieve encapsulation in Java −
1 . Declare the variables of a class as private.
2 . Provide public setter and getter methods
to modify and view the variables values.
Encapsulation
public class EncapTest {
private String name;
private String idNum;
public String getName() {
return name;
}
public String getIdNum() {
return idNum;
}
public void setName(String newName) {
name = newName;
}
public void setIdNum( String newId) {
idNum = newId;
}
}
Encapsulation Sample code
Method Constructor
1. Method expose the object
behavior.
1. Constructor initialize the state of
an object.
2. Method name may or may not be
same as the class name.
2. Constructor name must be same
as the class name.
3. . Method must have return
type ..
3. Constructor must not have return
type.
4. Method is invoked explicity . 4. Constructor is invoked implicity .
5. Compiler don’t provide method
any case.
5. Compiler provides a default
constructor if there's no constructor .
Difference Between Method and Constructor
Student
- Id : String
- name : String
- mark : String
+ student()
+ student(string:id)
Student
- Id : String
- name : String
- mark : String
+ student() : void
UML Diagram between Constructor and Method
Abstract
Abstract : when we don’t create an object
then it is called abstract .
1 . We use it before the concrete class .
2 . It has its own method .
3 . Abstract doesn’t have any body .
4 . Abstract classes cannot be instantiated, but
they can be subclassed.
5 . When an abstract class is subclassed, the
subclass usually provides implementations for all
of the abstract methods in its parent class.
Abstract class HelloAbstractWorld { }
Class AbstractDemo {
Public static void main (String ar [ ]) {
System.out.println(“Hello World”);
//HelloAbstractWorld obj = new HelloAbstractWorld
}
} /* HelloAbstractWorld is Abstract and cannot be
intantiated */
Simple Abstract Code
Thank you
Hope u don’t get bored !!

More Related Content

What's hot

What's hot (20)

java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Java lec class, objects and constructors
Java lec class, objects and constructorsJava lec class, objects and constructors
Java lec class, objects and constructors
 
Python Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | EdurekaPython Class | Python Programming | Python Tutorial | Edureka
Python Class | Python Programming | Python Tutorial | Edureka
 
javainheritance
javainheritancejavainheritance
javainheritance
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Oops in Java
Oops in JavaOops in Java
Oops in Java
 
Java Programming - Inheritance
Java Programming - InheritanceJava Programming - Inheritance
Java Programming - Inheritance
 
Oops in java
Oops in javaOops in java
Oops in java
 
Inner classes in java
Inner classes in javaInner classes in java
Inner classes in java
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Oop in kotlin
Oop in kotlinOop in kotlin
Oop in kotlin
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
OOPS in Java
OOPS in JavaOOPS in Java
OOPS in Java
 

Similar to Java

Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and PolymorphismKartikKapgate
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritancemcollison
 
Java classes and objects interview questions
Java classes and objects interview questionsJava classes and objects interview questions
Java classes and objects interview questionsDhivyashree Selvarajtnkpm
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsnishajj
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdfvenud11
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitishChaulagai
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 

Similar to Java (20)

Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Java Simple Notes
Java Simple NotesJava Simple Notes
Java Simple Notes
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Ruby object model
Ruby object modelRuby object model
Ruby object model
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
Java session2
Java session2Java session2
Java session2
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
Pi j3.1 inheritance
Pi j3.1 inheritancePi j3.1 inheritance
Pi j3.1 inheritance
 
Java classes and objects interview questions
Java classes and objects interview questionsJava classes and objects interview questions
Java classes and objects interview questions
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java oops interview questions
Top 10 java oops interview questionsTop 10 java oops interview questions
Top 10 java oops interview questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Top 10 java_oops_interview_questions
Top 10 java_oops_interview_questionsTop 10 java_oops_interview_questions
Top 10 java_oops_interview_questions
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
1669617800196.pdf
1669617800196.pdf1669617800196.pdf
1669617800196.pdf
 
Nitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptxNitish Chaulagai Java1.pptx
Nitish Chaulagai Java1.pptx
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 

Java

  • 1. Ahmed - 1 . Inheritance Polash - 2 . Polymorphism Aman - 3 . Overloading - 4 . Overriding Mustafiz - 5 . Construction - 6 . Encapsulation Galib - 7 . Diff between method n construction - 8 . Abstract Welcome to our presentation
  • 2. INTRODUCTION TO INHERITANCE • Java classes are used in several ways.This is basically by creating new classes,resuing the properties existing one. • The deriving the new class from an old one is called inheritance . • The old class is known as base class or super class. • The class who inherit the properties of the base class is called derived class or sub class.
  • 3. TYPES OF INHERITANCE • Single inheritance(one super class) • Multiple inheritance • Multilevel inheritance • Hierarchical inheritance
  • 4. DEFINE SUBCLASS • Class sub-classname extends super-classname • { • variable declaration ;// sub class variables • method declaration ;// sub class method • } • The keyword extends signifies that the properties of the super class are extended to the sub class.
  • 5. EXAMPLE OF SINGLE INHERITANCE • Class sum //Super class • { • int a=10; • int b=20; • void show() • { • System.out.println(“value of a :-” +a); • System.out.println(“value of a :-” +b); • } • } • Class sum2 extends sum //base class • { • public static void main(String args[]) • { • Sum2 obj =new sum2(); • Obj.show1(); } }
  • 6. Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object. Any Java object that can pass more than one IS-A test is considered to be polymorphic. Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object. Polymorphism
  • 7. Public class Dog extends Animal { Public void make sound () { System.out.println(“Woof !”); } Public void make sound (Boolean injured) { If (injured){ System.out.println(“Whimper”); } } Polymorphism sample code
  • 8. Definition: Overloading occurs when two or more methods in one class have the same method name but different parameters. Overloading
  • 9.
  • 10.
  • 11. Definition: Overriding means having two methods with the same method name and parameters. One of the methods is in the parent class and the other is in the child class. Overriding allows a child class to provide a specific implementation of a method that is already provided its parent class.
  • 12.
  • 13.
  • 14.  In the example above, the dog variable is declared to be a Dog. During compile time, the compiler checks if the Dog class has the bark() method. As long as the Dog class has the bark() method, the code compilers. At run-time, a Hound is created and assigned to dog. The JVM knows that dog is referring to the object of Hound, so it calls the bark() method of Hound. This is called Dynamic Polymorphism
  • 15. Constructor A constructor in Java is a block of code similar to a method that’s called when an instance of an object is created . Constructor are 2 type 1 . Default constructor (no-arg constructor) 2 . Parameterized constructor Default Parameterized Student obj = new Student ( ) Student obj = new Student ( name ) Difference between default and parameterized constructor
  • 16. Public class StudentResult { private string Full_Name ; private string Exam_Name ; private string Exam_Grade ; StudentResult( String name , String grade ) { Full_Name = name ; Exam_Grade = grade ; } Constructor Sample Code
  • 17. Encapsulation is one of the four fundamental OOP concepts. The other three are inheritance, polymorphism, and abstraction. Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit. ... Declare the variables of a class as private. To achieve encapsulation in Java − 1 . Declare the variables of a class as private. 2 . Provide public setter and getter methods to modify and view the variables values. Encapsulation
  • 18. public class EncapTest { private String name; private String idNum; public String getName() { return name; } public String getIdNum() { return idNum; } public void setName(String newName) { name = newName; } public void setIdNum( String newId) { idNum = newId; } } Encapsulation Sample code
  • 19. Method Constructor 1. Method expose the object behavior. 1. Constructor initialize the state of an object. 2. Method name may or may not be same as the class name. 2. Constructor name must be same as the class name. 3. . Method must have return type .. 3. Constructor must not have return type. 4. Method is invoked explicity . 4. Constructor is invoked implicity . 5. Compiler don’t provide method any case. 5. Compiler provides a default constructor if there's no constructor . Difference Between Method and Constructor
  • 20. Student - Id : String - name : String - mark : String + student() + student(string:id) Student - Id : String - name : String - mark : String + student() : void UML Diagram between Constructor and Method
  • 21. Abstract Abstract : when we don’t create an object then it is called abstract . 1 . We use it before the concrete class . 2 . It has its own method . 3 . Abstract doesn’t have any body . 4 . Abstract classes cannot be instantiated, but they can be subclassed. 5 . When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.
  • 22. Abstract class HelloAbstractWorld { } Class AbstractDemo { Public static void main (String ar [ ]) { System.out.println(“Hello World”); //HelloAbstractWorld obj = new HelloAbstractWorld } } /* HelloAbstractWorld is Abstract and cannot be intantiated */ Simple Abstract Code
  • 23. Thank you Hope u don’t get bored !!