Course Content

Course Content

Description

Introduction to OOP in Java

In our previous video of our ongoing Core Java course we have seen that what is Life Cycle of Thread in Java programming. Now in this video we are going to see everything about Object Oriented Programming in Java from scratch.

There are three Main Principles in OOP (Object Oriented Programming) in Java programming. Let’s see them individually !

First is Encapsulation in Java. It means putting all the variables together and the methods into a single unit called Class. Or we can say binding code & data together into a single unit is known as Encapsulation. For example, you must have seen a capsule; it is wrapped with different color medicines. Same way, in JAVA, class encapsulates data. This data are private and cannot be used outside the class.

Polymorphism in Java : If we break this term, poly means many & morphos means forms. When one method performs different tasks then that method is said to exhibit polymorphism. Like if I’ve cleaning method, I can use that in cleaning cars/home/kitchen wears/ cloths etc. There are two types of polymorphism in Java -
1. Compile time polymorphism : This is also called as Static polymorphism. It occurs when a method is overload; that is when the argument used in the method is changed.
2. Run time polymorphism : This is also called as Dynamic polymorphism. This occurs when the method itself is changed.

Method Overloading in Java : If a class has multiple methods by same name but different parameters, then it is called Method Overloading. Methods in Java are defined under class. It increase readability of the program. There are so many ways you can recognize method overloading – method’s name will be same, same return type etc.
Java Method Overloading is also known as Static Polymorphism. There are two ways to overload the method in Java programming -
First is by Changing Number of Arguments and Second is by Changing the Data type of Arguments.

Let’s see both of the methods one by one : -

Example of Changing Number of Arguments :
Class overload
{
Int ans;
Void sum(int a, int b)
{
Ans= a+b;
System.out.println( “addition is-> “+ans);
}
Void sum(int a, int b, int c)
{
Ans= a+b+c;
System.out.println(“Addition is “+ans);
}
Void sum(int a, int b, int c, int d)
{
Ans= a+b+c+d;
System.out.println(“Addition is “+ans);
}
Public static void main(String args[])
{
Overload a1= new overload();
a1.sum(10,2);
a1.sum(10,2,3);
a1.sum(10,2,3,4);
}
}
O/P :
Save this program as overload.Java
Now to run this program, open cmd and go to C drive. As shown in video, enter one by one element of path to reach your program. Compile the program by javac and if no error will be there then it will allow you to run the program. To run, write java overload. You will see, all addition answer will appear on your screen as 12, 15 & 19.

We saw example of Changing Number of arguments for overloading, now we will see Overloading by changing the data type :
Class load
{
Void disp(char c)
{
System.out.println(+c);
}
Void disp(int c)
{
System.out.println(+c);
}
Public static void main (string a[])
{
Load l1= new load();
l1.disp(‘a’);
l1.disp(1);
}
}
O/P :
Save it the program as load.java. Run the program as we did in last example. Enter file name and compile the program. Correct the errors if any happened and again run it. You will see in output, you will get ASCII value of a and integer 1. If you remove + before c, then you will get a as character.

Method Overriding in Java programming : In Method Overriding, method’s name, argument and data types are same. This is used in Inheritance. In this, if subclass (child class) has the same method as declared in the parent class, it is known as method overriding in java. Method overriding is used to provide specific implementation of a method that is already provided by its super class.
It is also known as Runtime Polymorphism. In this, inheritance provides such property that you can access all properties of super class in subclass. Let’s see example :
Class Human
{
Public void eat()
{
System.out.println(“Human is eating”);
}}
Class Boy extends Human{
Public void eat(){
System.out.println(“Boy is eating”);
Public static void main( String args[])
Boy b = new Boy();
b.eat();
}}
O/P :
Save this file as Boy.java and go to cmd prompt. Write Boy.java and run the program. You will see, it will show Boy is eating. Here, method’s return type is same, name is same; here one eat method is overriding another.

Inheritance in Java -
It means child class can use all parents’ class properties. Till now, we have seen a use of single class. Now we will see how to use multiple or multilevel class. In inheritance, it is mandatory to have at least two class. One is superclass/parent class and second is subclass/child class. Inheritance is a compile time mechanism. The relation between super & sub class is known as “is-a” relationship.

There are three types of Inheritance in Java programming :
1. Single level Inheritance in Java,
2. Multilevel Inheritance in Java, and
3. Hierarchical Inheritance in Java

In previous example, we saw using extent keyword, we can inherit one class to another. In java, multiple and hybrid inheritance is supported through interface only; otherwise it is not supported.

Single Level Inheritance in Java : As shown in video, A is a parent class of B and B is child class of A.
Let’s see example of Single Level Inheritance :
Class Parent{
Public void p1()
{
System.out.println(“Parent Class”);
}}
Class Child extends Parent{
Public void c1(){
System.out.println(“Child class”);
Public static void main( String args[])
{
Child c=new child();
c.p1();
c.c1();
}}
O/P :
Save it file as Child.java and run it on cmd prompt. In output, you will see child method and parent method is displayed.

Multi-level inheritance in Java : In Multilevel Inheritance, there should be three class. As shown in video, C is subclass of B and B is a sub class of A.
Let’s see an example of Multilevel Inheritance :
Class X {
Public void show()
{
System.out.println(“Class X method”);
}}
Class Y extends X{
Public void display(){
System.out.println(“Class y method”);
}}
Class Z extends Y
{
Public void output(){
System.out.println(“Class z method”);
}
Public static void main( String args[])
{
Z obj = new Z();
obj.show();
obj.display();
obj.output();
}}
O/P :
Save the program by Z.java and got to cmd prompt and run it. You will see, all three methods are displayed as we defined them.

Hierarchical Inheritance in Java : In Hierarchical Inheritance, one parent class will be inherited by many child classes. In this, we have to create object for every class. As shown in video, A is parent class and B,C and D are child class.
Let’s see an example of Hierarchical Inheritance :
Class A {
void show()
{
System.out.println(“Class a”);
}}
Class B extends A{
void display(){
System.out.println(“Classb”);
}}
Class C extends A
{
void output(){
System.out.println(“Class c”);
}}
Class test
Public static void main( String s[])
{
A aa=new A ();
aa.show();
B bb=new B();
bb.show();
bb.display();
C cc=new C();
cc.show();
cc.output();
}}
O/P :
Save this program by our main method name that is test. So entertest.java. Go to cmd prompt and run the file. In output, you will see objects of A class first. In B class we have define show and display both. In c class, you will see,‘a’ is came as inherited class.

Aggregation in Java programming :
If a class have an entity reference, it is known as Aggregation in Java. It is called as HAS-A relationship. Aggregation is used for reusability.
Let’s see example of Aggregation in Java :
Class Operation
{
Int square(int n) {
Return n*n; } }
Class Circle{
Operation op; //aggregation
Double pi= 3.14;
Double area (int radius){
Op=new Operation();
Int rsquare= op.square(radius); //code reusability
Return pi*rsquare;
}
Public static void main (String args[]){
Circle c=new Circle();
Double result = c.area(5);
System.out.println(result);
}
}
O/P :
Compile this program and see the output. You will get 78.5 as pi’s value is 3.14 and we are multiplying it by 5.

So, this was all about Object Oriented Programming in Java. Now to learn about Java Swing, we need to proceed further for next video.

FAQs

Recommended Courses

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere